package com.nsu.evaluation.controller;

import java.util.List;
import java.util.Map;

import com.nsu.evaluation.common.ResponseCode;
import com.nsu.evaluation.pojo.*;
import com.nsu.evaluation.service.impl.AdminUsersServiceImpl;
import com.nsu.evaluation.service.impl.CourseServiceImpl;
import com.nsu.evaluation.service.impl.RoleServiceImpl;
import com.nsu.evaluation.service.impl.StudentServiceImpl;
import com.nsu.evaluation.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.nsu.evaluation.common.Const;
import com.nsu.evaluation.common.ServerResponse;
import com.nsu.evaluation.service.AdminUsersService;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 * 
 * @author Eleven
 * 20180430 23:24
 *
 */
@Controller
@RequestMapping("/api/adminUsers/")
public class AdminUsersController {

	@Autowired
	private AdminUsersServiceImpl adminUsersServiceImpl;

	@Autowired
	private StudentServiceImpl studentServiceImpl;

	@Autowired
	private RoleServiceImpl roleServiceImpl;

	/**
	 * 跳转到管理员首页
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "toAdminIndex")
	public ModelAndView toAdminIndex(HttpServletRequest request,HttpSession session){
		ModelAndView modelAndView = new ModelAndView("/WEB-INF/page/adminuser/adminuserindex.jsp");
        AdminUsers adminUsers = (AdminUsers)session.getAttribute(Const.CURRENT_USER);
        modelAndView.addObject("userdata",adminUsers);
        modelAndView.addObject("userrole",session.getAttribute(Const.CURRENT_USER_ROLE));
		return modelAndView;
	}

	/**
	 * 跳转到评论管理员首页
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "toEvaluationIndex")
	public ModelAndView toEvaluationIndex(HttpServletRequest request,HttpSession session){
		ModelAndView modelAndView = new ModelAndView("/WEB-INF/page/evaluation/evaluationindex.jsp");
		AdminUsers adminUsers = (AdminUsers)session.getAttribute(Const.CURRENT_USER);
		modelAndView.addObject("userdata",adminUsers);
		modelAndView.addObject("userrole",session.getAttribute(Const.CURRENT_USER_ROLE));
		return modelAndView;
	}

	/**
	 * 跳转到用户管理界面
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "toUserManage")
	public ModelAndView toUserManage(HttpSession session) {
		ModelAndView modelAndView = new ModelAndView("/WEB-INF/page/adminuser/user_manage.jsp");
		AdminUsers adminUsers = (AdminUsers)session.getAttribute(Const.CURRENT_USER);
		if(adminUsers == null){
			//如果没有登录，返回登录界面
			ModelAndView modelAndView1 = new ModelAndView("/api/user/prepareToLogin");
			return modelAndView1;
		}
		Map resmap = adminUsersServiceImpl.getManagedAdminUsers(adminUsers).getData();
		if(resmap != null) {
			List<AdminUsers> adminUsersList = (List<AdminUsers>) resmap.get("userlist");
			List<String> roleList = (List<String>) resmap.get("rolelist");
			modelAndView.addObject("userlist", adminUsersList);
			modelAndView.addObject("rolelist", roleList);
		}
		return modelAndView;
	}

	/**
	 * 跳转到学生管理界面
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "toStudentManage")
	public ModelAndView toStudentManage(HttpSession session) {
		ModelAndView modelAndView = new ModelAndView("/WEB-INF/page/adminuser/student_manage.jsp");
		AdminUsers adminUsers = (AdminUsers)session.getAttribute(Const.CURRENT_USER);
		if(adminUsers == null){
			//如果没有登录，返回登录界面
			ModelAndView modelAndView1 = new ModelAndView("/api/user/prepareToLogin");
			return modelAndView1;
		}
		List<Student> studentList = adminUsersServiceImpl.getStudentList().getData();
		if(studentList != null) {
			modelAndView.addObject("userlist", studentList);
		}
		return modelAndView;
	}

	/**
	 * 跳转到角色管理界面
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "toRoleManage")
	public ModelAndView toRoleManage(HttpSession session) {
		ModelAndView modelAndView = new ModelAndView("/WEB-INF/page/adminuser/role_manage.jsp");
		AdminUsers adminUsers = (AdminUsers)session.getAttribute(Const.CURRENT_USER);
		if(adminUsers == null){
			//如果没有登录，返回登录界面
			ModelAndView modelAndView1 = new ModelAndView("/api/user/prepareToLogin");
			return modelAndView1;
		}
		List<Role> roleList = roleServiceImpl.getRoleList().getData();
		if(roleList != null) {
			modelAndView.addObject("rolelist", roleList);
		}
		return modelAndView;
	}

	/**
	 * 管理员用户登录
	 * @return
	 */
	@RequestMapping(value = "adminUserLogin")
	@ResponseBody
	public ServerResponse adminUserLogin(String username, String password,String roleType, HttpSession session){
		ServerResponse<AdminUsers> response = adminUsersServiceImpl.adminUserLogin(username,password);
		if(response.isSuccess()){
			session.setAttribute(Const.CURRENT_USER,response.getData());
			session.setAttribute(Const.CURRENT_USER_ROLE,roleType);
		}
		return response;
	}

	/**
	 * 管理员用户注销
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "adminUserLogout")
	@ResponseBody
	public ModelAndView adminUserLogout(HttpSession session){
		session.removeAttribute(Const.CURRENT_USER);
		ModelAndView modelAndView = new ModelAndView("/api/user/prepareToLogin");
		return modelAndView;
	}

	/**
	 * 个人信息修改
	 * @param adminUsers
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "updateAdminUsersInfo")
	@ResponseBody
	public ServerResponse<AdminUsers> updateAdminUsersInfo(AdminUsers adminUsers,HttpSession session){
		AdminUsers currentAdminUsers = (AdminUsers)session.getAttribute(Const.CURRENT_USER);
		if(null == currentAdminUsers){
			return ServerResponse.createByErrorMessage("用户尚未登录");
		}
		//如果用户没有输入密码就用原本的密码。
        if(adminUsers.getPassword() != null && !adminUsers.getPassword().equals("")){
		    adminUsers.setPassword(MD5Util.MD5EncodeUtf8(adminUsers.getPassword()));
        }else{
		    adminUsers.setPassword(currentAdminUsers.getPassword());
        }
		//设置用户ID为当前登录的id
		adminUsers.setId(currentAdminUsers.getId());
		ServerResponse<AdminUsers> response = adminUsersServiceImpl.updateAdminUsersInfo(adminUsers);
		return response;
	}

	/**
	 * 新增管理员用户
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "addAdminUsers")
	@ResponseBody
	public ServerResponse addAdminUsers(@RequestParam Map<String,String> data){
		return adminUsersServiceImpl.addAdminUsers(data);
	}

	/**
	 * 根据用户名查询用户数据
	 * @param adminUsers
	 * @return
	 */
	@RequestMapping(value="getUserByParams",method = RequestMethod.GET)
	@ResponseBody
	public ServerResponse<List<AdminUsers>> getUserByParams(AdminUsers adminUsers){
		return adminUsersServiceImpl.queryAdminUserByUserName(adminUsers);
	}

	/**
	 * 根据ID查询用户数据
	 * @return
	 */
    @RequestMapping(value="selectAdminUsersByPrimaryKey")
    @ResponseBody
    public ServerResponse<AdminUsers> selectAdminUsersByPrimaryKey(String adminUserId){
    	return adminUsersServiceImpl.selectByPrimaryKey(adminUserId);
    }

	/**
	 * 根据用户ID删除用户信息
	 * @param adminUserId
	 * @return
	 */
	@RequestMapping(value = "deleteAdminUsersByPrimaryKey")
    public ModelAndView deleteAdminUsersByPrimaryKey(String adminUserId){
		ServerResponse response = adminUsersServiceImpl.deleteAdminUserByPrimaryKey(adminUserId);
		//不管是否删除成功都返回到管理界面
		ModelAndView modelAndView = new ModelAndView("/api/adminUsers/toUserManage");
		modelAndView.addObject("deletemsg",response.getMsg());
		return modelAndView;
	}

	/**
	 * 修改用户信息
	 * @param adminUsers
	 * @return
	 */
	@RequestMapping(value = "updateAdminUsersByParams")
	@ResponseBody
	public ServerResponse<AdminUsers> updateAdminUsersByParams(AdminUsers adminUsers){
		return adminUsersServiceImpl.updateAdminUsersInfo(adminUsers);
	}

	/**
	 * 获取用户列表
	 * @return
	 */
	@RequestMapping(value = "getAdminUsersList")
	@ResponseBody
	public ServerResponse<List<AdminUsers>> getAdminUsersList(){
		return adminUsersServiceImpl.getAdminUsersList();
	}


	/**
	 * 新增学生用户
	 * @param student
	 * @return
	 */
	@RequestMapping(value = "addStudentUser")
	@ResponseBody
	public ServerResponse<Student> addStudentUser(Student student){
		return adminUsersServiceImpl.addStudentUser(student);
	}

	/**
	 * 根据姓名或学号搜索学生信息
	 * @param student
	 * @return
	 */
	@RequestMapping(value = "selectStudentByExample")
	@ResponseBody
	public ServerResponse<List<Student>> selectStudentByExample(Student student){
		return adminUsersServiceImpl.selectStudentByExample(student);
	}

	/**
	 * 根据学生Id删除学生
	 * @param studentId
	 * @return
	 */
	@RequestMapping(value = "deleteStudentByPrimaryKey")
	@ResponseBody
	public ModelAndView deleteStudentByPrimaryKey(String studentId){
		ServerResponse response = studentServiceImpl.deleteStudentById(studentId);
		//不管是否删除成功都返回到管理界面
		ModelAndView modelAndView = new ModelAndView("/api/adminUsers/toStudentManage");
		modelAndView.addObject("deletemsg",response.getMsg());
		return modelAndView;
	}

	/**
	 * 根据学生id修改学生信息
	 * @param student
	 * @return
	 */
	@RequestMapping(value = "updateStudentByPrimaryKey")
	@ResponseBody
	public ServerResponse<Student> updateStudentByPrimaryKey(Student student){
		return adminUsersServiceImpl.updateStudentByPrimaryKey(student);
	}

	/**
	 * 获取全部学生列表
	 * @return
	 */
	@RequestMapping(value = "getStudentList")
	@ResponseBody
	public ServerResponse<List<Student>> getStudentList(){
		return adminUsersServiceImpl.getStudentList();
	}

//	AdminUsers adminUsers = (AdminUsers)session.getAttribute(Const.CURRENT_USER);
//		if(adminUsers ==null){
//		return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
//	}

}
